package com.freesky.mobile.wechat.controller;

import com.freesky.common.core.controller.BaseController;
import com.freesky.mobile.wechat.config.WxCpConfiguration;
import me.chanjar.weixin.cp.api.WxCpDepartmentService;
import me.chanjar.weixin.cp.api.WxCpExternalContactService;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.api.WxCpUserService;
import me.chanjar.weixin.cp.bean.external.WxCpMsgTemplate;
import me.chanjar.weixin.cp.bean.external.msg.Link;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/wx/portal/test")
public class WxCpTestController extends BaseController {


//    @Autowired
//    @Qualifier("cpWechatUserService")
//    private CpWechatUserService cpWechatUserService;
//
//
//    @RequestMapping("/loadDepartList")
//    @ResponseBody
//    public ResultModel loadDepartList(HttpServletRequest request, HttpServletResponse response){
//        ResultModel result = new ResultModel(true);
//        try{
//            final WxCpService wxCpService = WxCpConfiguration.getCpService(9999999);
//            WxCpDepartmentService wxCpDepartmentService = wxCpService.getDepartmentService();
//            //cpWechatUserService.addCpDepart(wxCpDepartmentService.list(null));
//            result.setBody(wxCpDepartmentService.list(null));
//
//        }catch(Exception e){
//            result.setCode(Constant.ERROR);
//            result.setMsg("操作失败，系统异常");
//            e.printStackTrace();
//        }
//        return result;
//    }
//
//    //获取部门成员:https://work.weixin.qq.com/api/doc/90000/90135/90200
//    @RequestMapping("/loadSimpleList")
//    @ResponseBody
//    public ResultModel loadSimpleList(String departmentId,
//                                      HttpServletRequest request, HttpServletResponse response){
//        ResultModel result = new ResultModel(true);
//        try{
//            final WxCpService wxCpService = WxCpConfiguration.getCpService(1000001);
//            WxCpUserService wxCpUserService = wxCpService.getUserService();
//            result.setBody(wxCpUserService.listSimpleByDepartment(Long.parseLong(departmentId),true,0));
//
//        }catch(Exception e){
//            result.setCode(Constant.ERROR);
//            result.setMsg("操作失败，系统异常");
//            e.printStackTrace();
//        }
//        return result;
//    }
//
//    //获取客户列表:https://work.weixin.qq.com/api/doc/90000/90135/92113
//    @RequestMapping("/loadExternalContact")
//    @ResponseBody
//    public ResultModel loadExternalContact(String userId,
//                                           HttpServletRequest request, HttpServletResponse response){
//        ResultModel result = new ResultModel(true);
//        try{
//            final WxCpService wxCpService = WxCpConfiguration.getCpService(1000002);
//            WxCpExternalContactService wxCpExternalContactService = wxCpService.getExternalContactService();
//
//            result.setBody(wxCpExternalContactService.listExternalContacts(userId));
//
//        }catch(Exception e){
//            result.setCode(Constant.ERROR);
//            result.setMsg("操作失败，系统异常");
//            e.printStackTrace();
//        }
//        return result;
//    }
//
//    //获取客户详情:https://work.weixin.qq.com/api/doc/90000/90135/92113
//    @RequestMapping("/getExternalContact")
//    @ResponseBody
//    public ResultModel getExternalContact(String externalUserId,
//                                           HttpServletRequest request, HttpServletResponse response){
//        ResultModel result = new ResultModel(true);
//        try{
//            final WxCpService wxCpService = WxCpConfiguration.getCpService(1000002);
//            WxCpExternalContactService wxCpExternalContactService = wxCpService.getExternalContactService();
//            result.setBody(wxCpExternalContactService.getContactDetail(externalUserId));
//
//        }catch(Exception e){
//            result.setCode(Constant.ERROR);
//            result.setMsg("操作失败，系统异常");
//            e.printStackTrace();
//        }
//        return result;
//    }
//
//    //获取客户详情:https://work.weixin.qq.com/api/doc/90000/90135/92113
//    @RequestMapping("/addMsgTemplate")
//    @ResponseBody
//    public ResultModel addMsgTemplate(HttpServletRequest request, HttpServletResponse response){
//        ResultModel result = new ResultModel(true);
//        try{
//            final WxCpService wxCpService = WxCpConfiguration.getCpService(1000002);
//            WxCpExternalContactService wxCpExternalContactService = wxCpService.getExternalContactService();
//
//            WxCpMsgTemplate wxCpMsgTemplate = new WxCpMsgTemplate();
//            wxCpMsgTemplate.setChatType("single");
//            List<String> externalUserid = new ArrayList<>();
//            externalUserid.add("wmeT8XCQAAdUHHxspCI287hSHKjr0JHw");
//            externalUserid.add("wmeT8XCQAAEmuiksUuniARO3ClPVRGiw");
//            externalUserid.add("wmeT8XCQAAiZW81o3_h5kYWBjkHZx_xA");
//            externalUserid.add("wmeT8XCQAAdCK4SLZWGA1utNWYYhOXlg");
//            wxCpMsgTemplate.setExternalUserid(externalUserid);
//
//            Link link = new Link();
//            link.setTitle("消费达标红包测试");
//            link.setDesc("可领取10元的消费达标红包!");
//            link.setPicUrl("");
//            link.setUrl("https://card.zgrongyidui.com/reach/index.html");
//            wxCpMsgTemplate.setLink(link);
//            result.setBody(wxCpExternalContactService.addMsgTemplate(wxCpMsgTemplate));
//
//        }catch(Exception e){
//            result.setCode(Constant.ERROR);
//            result.setMsg("操作失败，系统异常");
//            e.printStackTrace();
//        }
//        return result;
//    }

}
